using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class GamePool
{
	public Dictionary<int, GamePoolObject> GamePoolObjectDict;

	private int _counter;

	private GameObject _poolHolder;

	private Dictionary<GameObject, int> PrefabToObjectIdLink;

	public GamePool(GameObject iGamePoolHolder, string iPoolName)
	{
		_poolHolder = new GameObject(iPoolName);
		_poolHolder.transform.parent = iGamePoolHolder.transform;
	}

	public int AddGameObjectToPool(GameObject iGameObject, int iCount, GamePoolSettings iGamePoolSettings)
	{
		if (GamePoolObjectDict == null)
		{
			GamePoolObjectDict = new Dictionary<int, GamePoolObject>();
			PrefabToObjectIdLink = new Dictionary<GameObject, int>();
		}
		if (PrefabToObjectIdLink.ContainsKey(iGameObject))
		{
			return GamePoolObjectDict[PrefabToObjectIdLink[iGameObject]].ObjectId;
		}
		_counter++;
		GamePoolObjectDict.Add(_counter, new GamePoolObject(_poolHolder, _counter, iGameObject, iCount, iGamePoolSettings));
		PrefabToObjectIdLink.Add(iGameObject, _counter);
		return _counter;
	}

	public GameObject GetGameObjectById(int iObjectId)
	{
		if (GamePoolObjectDict == null || !GamePoolObjectDict.ContainsKey(iObjectId))
		{
			BCWDebug.LogError("ObjectId " + iObjectId + " not found in pool " + _poolHolder.name);
			return null;
		}
		return GamePoolObjectDict[iObjectId].GetFreeObject();
	}

	public GameObject GetGameObjectByPrefab(GameObject iPrefab)
	{
		if (PrefabToObjectIdLink == null || GamePoolObjectDict == null || !PrefabToObjectIdLink.ContainsKey(iPrefab))
		{
			BCWDebug.LogError("Object " + iPrefab.name + " not found in pool " + _poolHolder.name);
			return null;
		}
		int num = PrefabToObjectIdLink[iPrefab];
		if (!GamePoolObjectDict.ContainsKey(num))
		{
			BCWDebug.LogError("ObjectId " + num + " not found in pool " + _poolHolder.name);
			return null;
		}
		return GamePoolObjectDict[num].GetFreeObject();
	}

	public void RemoveGameObjectFromPool(int iObjectId)
	{
		if (GamePoolObjectDict == null || !GamePoolObjectDict.ContainsKey(iObjectId))
		{
			BCWDebug.LogError("ObjectId " + iObjectId + " not found in pool " + _poolHolder.name + " for remove");
		}
		else
		{
			GamePoolObjectDict[iObjectId].Clear();
			GamePoolObjectDict.Remove(iObjectId);
		}
	}

	public void Clear()
	{
		if (GamePoolObjectDict != null)
		{
			int num;
			for (num = 0; num < GamePoolObjectDict.Count; num++)
			{
				RemoveGameObjectFromPool(GamePoolObjectDict.ElementAt(num).Key);
				num--;
			}
		}
		Object.Destroy(_poolHolder);
		_poolHolder = null;
	}
}
